<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>OpenMesh: Using iterators and circulators</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="logo_align.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="rwth_vci_rgb.jpg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">OpenMesh
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('a04337.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Using iterators and circulators </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This examples shows:</p>
<ul>
<li>How to use iterators,</li>
<li>How to use circulators.</li>
</ul>
<p>This example is the first version of the simple mesh smoother. Here we will introduce <em>iterators</em> and <em>circulators</em>. These two concepts provide functionality to linearly enumerate e.g. all vertices of a mesh, and to circulate around a vertex, i.e. to enumerate all its one-ring neighbors. For a more detailed description, see <a class="el" href="a04328.html">Mesh Iterators and Circulators</a>.</p>
<p>First we have to define the mesh type we want to use. This time we use a triangle mesh instead of a polygonal mesh:</p>
 <div class="fragment"><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh&gt;</span></div><div class="line"><span class="keyword">typedef</span> <a class="code" href="a02377.html">OpenMesh::TriMesh_ArrayKernelT&lt;&gt;</a>  MyMesh;</div></div><!-- fragment --></p>
<p><br />
We read the mesh to be smoothed from a file:</p>
<p><div class="fragment"><div class="line">  <span class="keywordflow">if</span> ( ! <a class="code" href="a01236.html#ac417960ec28ffdd7e7003a2b1ebb2d1c">OpenMesh::IO::read_mesh</a>(mesh, argv[2]) )</div></div><!-- fragment --></p>
<p><br />
One smoothing iteration is done in two steps: </p><ol>
<li>
For each vertex: calculate the barycenter of its one-ring neighbors. </li>
<li>
For each vertex: move the vertex to the computed barycenter. </li>
</ol>
<p>This can easily be implemented using vertex iterators. The mesh provides begin and end iterators by <code>vertices_begin()</code> and <code>vertices_end()</code>.</p>
<p><div class="fragment"><div class="line">  MyMesh::VertexIter          v_it, v_end(mesh.vertices_end());</div><div class="line">    <span class="keywordflow">for</span> (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)</div></div><!-- fragment --></p>
<p><br />
For calculating the barycenter, we have to iterate through the one-ring neighborhood of the current vertex. This functionality is provided by the <code>VertexVertexIter</code>:</p>
 <div class="fragment"><div class="line">  <a class="code" href="a02389.html#adc7d422e2ce13141e8291f7d29f7ecef">MyMesh::VertexVertexIter</a>    vv_it;</div><div class="line">      <span class="keywordflow">for</span> (vv_it=mesh.vv_iter( *v_it ); vv_it.is_valid(); ++vv_it)</div></div><!-- fragment --></p>
<p><br />
Now we can calculate the barycenters for each vertex and store them in the array <code>cogs</code>:</p>
 <div class="fragment"><div class="line">  std::vector&lt;MyMesh::Point&gt;  cogs;</div><div class="line">    <span class="keywordflow">for</span> (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)</div><div class="line">    {</div><div class="line">      cog[0] = cog[1] = cog[2] = valence = 0.0;</div><div class="line">      </div><div class="line">      <span class="keywordflow">for</span> (vv_it=mesh.vv_iter( *v_it ); vv_it.is_valid(); ++vv_it)</div><div class="line">      {</div><div class="line">        cog += mesh.point( *vv_it );</div><div class="line">        ++valence;</div><div class="line">      }</div><div class="line"></div><div class="line">      cogs.push_back(cog / valence);</div><div class="line">    }</div></div><!-- fragment --></p>
<p><br />
After we have calculated the barycenters all that is left to do is to move the vertices to the corresponding barycenters. The complete source code is listed below.</p>
<div class="fragment"><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;vector&gt;</span></div><div class="line"><span class="comment">// -------------------- OpenMesh</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/IO/MeshIO.hh&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">typedef</span> <a class="code" href="a02377.html">OpenMesh::TriMesh_ArrayKernelT&lt;&gt;</a>  MyMesh;</div><div class="line"></div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main(<span class="keywordtype">int</span> argc, <span class="keywordtype">char</span> **argv)</div><div class="line">{</div><div class="line">  MyMesh  mesh;</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// check command line options</span></div><div class="line">  <span class="keywordflow">if</span> (argc != 4) </div><div class="line">  {</div><div class="line">    std::cerr &lt;&lt; <span class="stringliteral">&quot;Usage:  &quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; #iterations infile outfile\n&quot;</span>;</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// read mesh from stdin</span></div><div class="line">  <span class="keywordflow">if</span> ( ! <a class="code" href="a01236.html#ac417960ec28ffdd7e7003a2b1ebb2d1c">OpenMesh::IO::read_mesh</a>(mesh, argv[2]) )</div><div class="line">  {</div><div class="line">    std::cerr &lt;&lt; <span class="stringliteral">&quot;Error: Cannot read mesh from &quot;</span> &lt;&lt; argv[2] &lt;&lt; std::endl;</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// this vector stores the computed centers of gravity</span></div><div class="line">  std::vector&lt;MyMesh::Point&gt;  cogs;</div><div class="line">  std::vector&lt;MyMesh::Point&gt;::iterator cog_it;</div><div class="line">  cogs.reserve(mesh.n_vertices());</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// smoothing mesh argv[1] times</span></div><div class="line">  MyMesh::VertexIter          v_it, v_end(mesh.vertices_end());</div><div class="line">  <a class="code" href="a02389.html#adc7d422e2ce13141e8291f7d29f7ecef">MyMesh::VertexVertexIter</a>    vv_it;</div><div class="line">  <a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>               cog;</div><div class="line">  <a class="code" href="a02389.html#a15372f27edf521d8e9028e2abccae342">MyMesh::Scalar</a>              valence;</div><div class="line">  <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>                i, N(atoi(argv[1]));</div><div class="line"></div><div class="line"></div><div class="line">  <span class="keywordflow">for</span> (i=0; i &lt; N; ++i)</div><div class="line">  {</div><div class="line">    cogs.clear();</div><div class="line">    <span class="keywordflow">for</span> (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)</div><div class="line">    {</div><div class="line">      cog[0] = cog[1] = cog[2] = valence = 0.0;</div><div class="line">      </div><div class="line">      <span class="keywordflow">for</span> (vv_it=mesh.vv_iter( *v_it ); vv_it.is_valid(); ++vv_it)</div><div class="line">      {</div><div class="line">        cog += mesh.point( *vv_it );</div><div class="line">        ++valence;</div><div class="line">      }</div><div class="line"></div><div class="line">      cogs.push_back(cog / valence);</div><div class="line">    }</div><div class="line">    </div><div class="line">    <span class="keywordflow">for</span> (v_it=mesh.vertices_begin(), cog_it=cogs.begin(); </div><div class="line">         v_it!=v_end; ++v_it, ++cog_it)</div><div class="line">      <span class="keywordflow">if</span> ( !mesh.is_boundary( *v_it ) )</div><div class="line">        mesh.set_point( *v_it, *cog_it );</div><div class="line">  }</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// write mesh to stdout</span></div><div class="line">  <span class="keywordflow">if</span> ( ! <a class="code" href="a01236.html#a18286a792ebf896872f5e26182fc5c7b">OpenMesh::IO::write_mesh</a>(mesh, argv[3]) )</div><div class="line">  {</div><div class="line">    std::cerr &lt;&lt; <span class="stringliteral">&quot;Error: cannot write mesh to &quot;</span> &lt;&lt; argv[3] &lt;&lt; std::endl;</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line"></div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<hr>
<address>
<small>
<a href="http://www.rwth-graphics.de" style="text-decoration:none;">
</a>
Project <b>OpenMesh</b>, 
&copy;&nbsp; Computer Graphics Group, RWTH Aachen.
Documentation generated using 
<a class="el" href="http://www.doxygen.org/index.html">
<b>doxygen</b>
</a>.
</small>
</address>
</body>
</html>
